<?php

namespace Admin\Controller;

class GoodController extends AdminController
{

    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }

    public function index()
    {
        $res = M('good')->select();
        $this->assign('_list', $res);
        $aaa = M('category')->getfield('id,title');
        $this->assign('aaa', $aaa);
        $this->display();
    }

    public function add()
    {
        if (IS_GET) {
            $res = M('category')->where(['pid' => 0])->select();
            $this->assign('res', $res);
            $this->display();
        } else {
            $_POST['pic'] = $this->upload('pic');
            $form         = M('good');
            $data         = $form->create();

            if ($data) {
                $form->add($data);
                $this->success('添加成功', U('index'));
            } else {
                $this->error($form->getError());
            }
        }
    }

    public function ajaxxiala()
    {
        $pid = I('get.pid');
        $res = M('category')->where(['pid' => $pid])->select();
        if ($res) {
            $data['status'] = 1;
            $data['info']   = '成功';
            $this->assign('res', $res);
            $data['res'] = $this->fetch('dongdongda');
        } else {
            $data['status'] = 0;
            $data['info']   = '失败';
            $data['res']    = null;
        }

        $this->ajaxReturn($data);
    }

    public function del($id)
    {
        $res = M('good')->where(['id' => $id])->delete();
        if ($res) {
            $this->success('大吉大利，今晚吃鸡', U('index'));
        }
    }

    public function update($id)
    {
        if (IS_GET) {
            $res_good = M('good')->where(['id' => I('get.id')])->find();
            $this->assign('res', $res_good);
            $res_cate = M('category')->where(['pid' => 0])->select();
            $this->assign('res_cate', $res_cate);
            $this->display();
        } else {
            $res = M('good')->where(['id' => I('get.id')])->find();
            $this->assign('res', $res);
            $_POST['pic'] = $this->upload('pic');
            $form         = M('good');
            $data         = $form->create();

            if ($data) {
                $form->where(['id' => I('post.id')])->save();
                $this->success('修改成功', U('index'));
            } else {
                $this->error($form->getError());
            }
        }
    }

    public function attrclass()
    {

        $this->assign('id', I('get.id'));
        $res = M('attrclass')->where(['gid' => I('get.id')])->select();
        $this->assign('_list', $res);
        $aaa = M('good')->getfield('id,title');
        $this->assign('aaa', $aaa);
        $this->display();
    }

    public function attrclass_add()
    {

        if (IS_GET) {
            $this->assign('gid', I('get.gid'));
            $this->display();
        } else {

            $res = M('attrclass')->add(I('post.'));
            if ($res) {
                $this->success('大吉大利，今晚吃鸡', U('index'));
            } else {
                $this->error('添加失败');
            }
        }

    }

    public function attrclass_del()
    {
        $res = M('attrclass')->where(['id' => I('get.id')])->delete();
        if ($res) {
            $this->success('大吉大利，今晚吃鸡', U('index'));
        }

    }

    public function attrlist()
    {
        $res = M('attrclass')->select();
        $this->assign('_list', $res);
        $aaa = M('good')->getfield('id,title');
        $this->assign('aaa', $aaa);
        $this->display();
    }

    public function attrval()
    {

        $this->assign('classid', I('get.classid'));
        $res = M('attrval')->where(['classid' => I('get.classid')])->select();
        $this->assign('_list', $res);

        $bbb = M('attrclass')->getfield('id,classname');
        $this->assign('bbb', $bbb);

        $aaa = M('good')->getfield('id,title');
        $this->assign('aaa', $aaa);

        $bbb = M('attrval')->where(['classid' => $res['id']])->select();
        $this->display();
    }

    public function attrval_add()
    {
        if (IS_GET) {
            var_dump(I('get.classid'));
            $this->assign('classid', I('get.classid'));
            $this->display();
        } else {

            $res         = M('attrclass')->where(['id' => I('post.classid')])->find();
            $res_attrval = M('attrval')->add(['vname' => I('vname'), 'classid' => I('post.classid'), 'gid' => $res['gid']]);
            if ($res_attrval) {
                $this->success('大吉大利', U('attrlist'));
            } else {
                $this->error('编辑失败');
            }
            $this->display();
        }

    }
    public function attrval_del()
    {
        $res = M('attrval')->where(['id' => I('get.id')])->delete();
        if ($res) {
            $this->success('删除成功', U('attrlist'));
        } else {
            $this->error('删除失败煞笔');
        }
    }

    public function CartesianProduct($sets)
    {
        // 保存结果
        $result = array();
        // 循环遍历集合数据
        for ($i = 0, $count = count($sets); $i < $count - 1; $i++
        ) {
            // 初始化
            if ($i == 0) {
                $result = $sets[$i];
            }
            // 保存临时数据
            $tmp = array();
            // 结果与下一个集合计算笛卡尔积
            foreach ($result as $res) {
                foreach ($sets[$i + 1] as $set) {
                    $tmp[] = $res . ',' . $set;
                }
            }
            // 将笛卡尔积写入结果
            $result = $tmp;
        }
        return $result;
    }

    public function attrprice()
    {
        if (IS_GET) {
            $val = M('attrval')->where(['gid' => I('get.id')])->select();
            $arr = [];
            $vid = [];
            
            foreach ($val as $v) {
                $arr[$v['classid']][] = $v['vname'];
                $vid[$v['classid']][] = $v['id'];
            }
            $name = array_values($arr);
            $id   = array_values($vid);
            $bb   = $this->CartesianProduct($name);
            $cc   = $this->CartesianProduct($id);
            $this->assign('gid', I('get.id'));
      
            $this->assign('res', $bb);
            $this->assign('vid', $cc);
            $this->display();
        } else {
          
            $I   = I('post.');
            $arr = [];
            foreach ($I['num'] as $k =>$v) {
                       $tmp = [];
                    $tmp['gid']     = $I['gid'];
                    $tmp['attrvid'] = $I['attrvid'][$k];
                    $tmp['price']   = $I['price'][$k];
                    $tmp['num']     = $I['num'][$k];
                    $tmp['attrvid_name']     = $I['attrvid_name'][$k];
                    $arr[] = $tmp; 
            }
            
            $wang = M('attrprice')->Addall($arr);
            if ($wang) {
                $this->success('大吉大利');
            } else {        
                $this->error('失败了');
            }
        }
    }

    public function price(){
      
      $res=M('attrprice')->where(['gid'=>I('get.gid')])->select();
      $gid=M('good')->where(['id'=>I('get.gid')])->find();
     
         $this->assign('aa',$gid['title']);
      $this->assign('_list',$res);
      $this->display();
    }









}
